package com.fzrskj.cloud.free.user.dao;

import com.fzrskj.cloud.free.core.utils.CloudDbDrvUtils;
import com.fzrskj.cloud.free.user.bean.UserTask;
import ms.core.consts.SysErr;
import ms.core.db.IDbDrv;
import ms.core.object.CObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class UserTaskDao extends CObject{
	//private declarations
	private IDbDrv db=null;

	//constructor function to initialize database access object
	public UserTaskDao(){
		db= CloudDbDrvUtils.getDbUtilsDrv();
	}

	//to free resource
	@Override
	public void dispose(){
		db.closeConnection();
	}

	//to add new record to database by bean
	public int addUserTask(UserTask obj){
		String sql = "insert into user_task(id,productCode,businessId,sendTime,expiryDays,type,userId,userName) values(?,?,?,?,?,?,?,?);";
		Object[] prm={obj.getId(),obj.getProductCode(),obj.getBusinessId(),obj.getSendTime(),obj.getExpiryDays(),obj.getType(),obj.getUserId(),obj.getUserName()};
		int ret = db.executeSql(sql,prm);
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}
	
	/**
	 * 查询到期任务是否已发送
	 * @param cdt
	 * @return
	 */
	public UserTask queryEndTime(Map<String, Object> cdt){
		StringBuffer sb = new StringBuffer("select * from user_task where 1=1 ");
		List<Object> list = new ArrayList<Object>();
		for(Map.Entry<String, Object> c: cdt.entrySet()){
			if(c.getKey().equals("sendTime")){
				sb.append(" and Date(sendTime)=Date(?)");
				list.add(c.getValue());
			}else {
				sb.append(" and "+c.getKey()+"=?");
				list.add(c.getValue());
			}
		}
		Object[] params = new Object[list.size()];
		for (int i = 0; i < list.size(); i++) {
			params[i] = list.get(i);
		}
		UserTask ret = db.queryForBean(sb.toString(), UserTask.class, params);
		this.setErrCode(db.getErrCode());
		return ret;
	}

	//to add new record to database by map
	public int addUserTask(Map<String, Object> values){
		int ret = db.insertRecord("user_task",values);
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	//to delete table record by id
	public int delUserTask(int id){
		String sql="delete from user_task where id=?";
		int ret = db.executeSql(sql,new Object[]{id});
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	/**
	 * 根据条件删除数据
	 * @param businessId
	 * @return
	 */
	public int delUserTask(String businessId){
		String sql = "delete from user_task where businessId=?";
		int ret = db.executeSql(sql,new Object[]{businessId});
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	//to modify table record by values and condition
	public int modUserTask(Map<String, Object> values, Map<String, Object> cdt){
		int ret = db.modifyRecord("user_task", values, cdt);
		String code = db.getErrCode();
		this.setErrCode(code);
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}

		return ret;
	}

	//to query UserTask record count
	public int queryUserTask(Map<String, Object> cdt){
		//TODO please verify count field name
		int ret = db.queryRecordCount("user_task", cdt);
		this.setErrCode(db.getErrCode());
		return ret;
	}

	//to query UserTaske for record list with condition,if page or pagesize less than 0, means without page
	public List<UserTask> queryUserTaskList(Map<String, Object> cdt, int page, int pagesize){
		List<UserTask> ret = db.queryForBeans("user_task", "*", " id desc ", UserTask.class, cdt, page, pagesize);
		String code=db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}
}